package com.jack.online.study.controller.edu.admin;

import com.jack.online.study.common.base.result.R;
import com.jack.online.study.model.edu.dao.Video;
import com.jack.online.study.service.edu.VideoService;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 课程视频 前端控制器
 * </p>
 *
 * @author jack
 * @since 2024-04-17
 */
@CrossOrigin
@RestController
@RequestMapping("/admin/edu/video")
public class VideoController {

    @Resource
    private VideoService videoService;


    /**
     * 新增课时
     */
    @PostMapping("/save")
    public R save(@RequestBody Video video){

        boolean result = videoService.save(video);
        if(result){
            return R.ok().message("保存成功");
        }else {
            return R.error().message("保存失败");
        }

    }

    /**
     * 根据id修改课时信息
     * @param video
     * @return
     */
    @PutMapping("update")
    public R updataVideoId(@RequestBody Video video){
        boolean result = videoService.updateById(video);
        if(result){
            return R.ok().message("修改成功");
        }else {
            return R.error().message("数据不存在");
        }
    }

    /**
     * 根据id删除课时
     * @param id 课时id
     */
    @DeleteMapping("remove/{id}")
    public R remove(@PathVariable String id){

        // 删除视频：VOD
        //在此处调用vod中的删除视频文件的接口,不想写在controller层
//        Video video = videoService.getById(id);
//        vodMediaService.removeVideo(video.getVideoSourceId());

        //删除视频
        videoService.removeMediaVideoById(id);

        boolean result = videoService.removeById(id);
        if(result){
            return R.ok().message("删除成功");
        }else {
            return R.error().message("删除失败");
        }
    }

    /**
     * 根据id查询课时
     * @param id 课时id
     */
    @GetMapping("get/{id}")
    public R getById(@PathVariable String id){

        Video video = videoService.getById(id);

        if(video != null){
            return R.ok().data("item",video);
        }else {
            return R.error().message("数据不存在");
        }
    }

}
